import * as XLSX from "xlsx";
export default function exportExcelTable(json, name, titleArr, sheetName,fields) {
    let data = new Array();
    if (!Array.isArray(json)) return console.warn('数据请传入数组');
    if (!Array.isArray(titleArr)) return console.warn('标题请传入数组');
    if (!Array.isArray(fields)) return console.warn('字段请传入数组');
    data=json.map(obj=>{
      return fields.map(field=>{
        return obj[field]
      })
    })
    data.splice(0, 0,  titleArr);
    // fields为英文字段表头,一般不需要，需要直接把下面注释打开即可
    // data.splice(0, 0, fields);
    
    const ws = XLSX.utils.aoa_to_sheet(data);
    const wb = XLSX.utils.book_new();
    // 此处隐藏英文字段表头
    let wsrows = [{ hidden: true }];
    ws['!rows'] = wsrows; // ws - worksheet
    XLSX.utils.book_append_sheet(wb, ws, sheetName);
    /* generate file and send to client */
    XLSX.writeFile(wb, name + '.xls');
  }